package com.erp.gather.utils;

import cn.hutool.core.lang.Dict;
import cn.hutool.http.HttpUtil;
import com.erp.gather.config.ChatGptConfig;
import com.erp.gather.config.UrlConfig;
import com.ruoyi.common.core.utils.JsonUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChatGptUtils {

	private static final ChatGptConfig chatGptConfig = SpringUtils.getBean(ChatGptConfig.class);
	private static final UrlConfig urlConfig = SpringUtils.getBean(UrlConfig.class);

	// 翻译
	public static String translate(String words) {
		if (isContainChinese(words)) {
			if (StringUtils.isNotEmpty(words)) {
				try {
					String res = HttpUtil.get(
							urlConfig.getHandle() + "/translate/translateText?source=" + words + "&scene=description");
					Dict parseMap = JsonUtils.parseMap(res);
					String data = (String) parseMap.get("data");
					return data;
				} catch (Exception e) {
					return "";
				}
			} else {
				return "";
			}
		} else {
			return words;
		}
	}

	// 提取标题关键字
	public static String extractTitle(String words) {
		if (StringUtils.isNotEmpty(words)) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("words", words);
				String post = HttpUtil.post(chatGptConfig.getEndpoint() + chatGptConfig.getExtractTitleUrl(), map);
				Dict parseMap = JsonUtils.parseMap(post);
				String data = (String) parseMap.get("data");
				data = data.replaceAll("：", "").replaceFirst("\n\n", "").replace(".", "");
				if (data.startsWith("\""))
					data = data.substring(1);
				if (data.endsWith("\""))
					data = data.substring(0, data.length() - 1);
				return data;
			} catch (Exception e) {
				return "";
			}
		} else {
			return "";
		}
	}

	// 提取详情关键字
	public static String extractDescription(String words) {
		if (StringUtils.isNotEmpty(words)) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("words", words);
				String post = HttpUtil.post(chatGptConfig.getEndpoint() + chatGptConfig.getExtractDescriptionUrl(),
						map);
				Dict parseMap = JsonUtils.parseMap(post);
				String data = (String) parseMap.get("data");
				data = data.replaceAll("：", "").replaceFirst("\n\n", "").replace(".", "");
				if (data.startsWith("\""))
					data = data.substring(1);
				if (data.endsWith("\""))
					data = data.substring(0, data.length() - 1);
				return data;
			} catch (Exception e) {
				return "";
			}
		} else {
			return "";
		}
	}

	// 根据关键字生成标题
	public static String createTitle(String words) {
		if (StringUtils.isNotEmpty(words)) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("words", words);
				String post = HttpUtil.post(chatGptConfig.getEndpoint() + chatGptConfig.getCreateTitleUrl(), map);
				Dict parseMap = JsonUtils.parseMap(post);
				String data = (String) parseMap.get("data");
				data = data.replaceAll("：", "").replaceFirst("\n\n", "");
				if (data.startsWith("\"\"")) {
					data = data.substring(2);
				} else if (data.startsWith("\"")) {
					data = data.substring(1);
				}
				if (data.endsWith("\""))
					data = data.substring(0, data.lastIndexOf("\""));
				data = data.replace("!", "");
				if (data.length() > 128)
					data = data.substring(0, 127);
				return data;
			} catch (Exception e) {
				return "";
			}
		} else {
			return "";
		}
	}

	// 根据关键字生成详情
	public static String createProductDescription(String words) {
		if (StringUtils.isNotEmpty(words)) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("words", words);
				String post = HttpUtil
						.post(chatGptConfig.getEndpoint() + chatGptConfig.getCreateProductDescriptionUrl(), map);
				Dict parseMap = JsonUtils.parseMap(post);
				String data = (String) parseMap.get("data");
				data = data.replaceAll("：", "").replaceFirst("\n\n", "");
				if (data.startsWith("\"\"")) {
					data = data.substring(2);
				} else if (data.startsWith("\"")) {
					data = data.substring(1);
				}
				if (data.endsWith("\""))
					data = data.substring(0, data.lastIndexOf("\""));
				data = data.replace("!", "");
				return data;
			} catch (Exception e) {
				return "";
			}
		} else {
			return "";
		}
	}

	/**
	 * 字符串是否包含中文
	 *
	 * @param str 待校验字符串
	 * @return true 包含中文字符 false 不包含中文字符
	 */
	public static boolean isContainChinese(String str) {

		if (StringUtils.isNotEmpty(str)) {
			Pattern p = Pattern.compile("[\u4E00-\u9FA5]");
			Matcher m = p.matcher(str);
			if (m.find()) {
				return true;
			}
		}
		return false;
	}
}
